//
//  FPCinemaViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPCinemaViewController.h"
#import "FPCinemaTableViewCell.h"
#import "FPCinemaCollectionViewCell.h"
#import "FPAllTableViewController.h"
#import "FPDetailCinemaTableViewController.h"
#import "FPCinemaModel.h"
//#import "FPNearbyTableViewController.h"
//#import "FPPriceTableViewController.h"

@interface FPCinemaViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UITableViewDataSource,UITableViewDelegate,SDCycleScrollViewDelegate>
{
    BOOL _isClick;// 是否点击
}
@property (nonatomic, strong) FPAllTableViewController *fpAllTVC;
//@property (nonatomic, strong) FPNearbyTableViewController *fpNearbyTVC;//---> 备用
//@property (nonatomic, strong) FPPriceTableViewController *fpPriceTVC;//---> 备用
@property (nonatomic, strong) NSArray *titleArray;// 存放标题


@end

@implementation FPCinemaViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 创建collectionView
    [self B_collectionViewButton];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [self.view addSubview:view];
    
    // Do any additional setup after loading the view.
    // 创建tableView
    [self B_tableView];
}

/*=========================================创建视图=============================================*/
#pragma mark - 创建视图
// 创建collectionView
- (void)B_collectionViewButton
{
    self.titleArray = @[@"全部",@"附近",@"价格"];
    // 创建布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30) collectionViewLayout:layout];
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
}

// 创建tableView
- (void)B_tableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight -94) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 90.0;
    
    self.tableView.tableHeaderView = [self buildScrollView];
    [self.view addSubview:_tableView];
}

// 轮播图
- (UIView *)buildScrollView
{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 160)];
    
    NSArray *images = @[[UIImage imageNamed:@"1.jpg"],
                        [UIImage imageNamed:@"2.jpg"],
                        [UIImage imageNamed:@"3.jpg"],
                        [UIImage imageNamed:@"4.jpg"],
                        [UIImage imageNamed:@"5.jpg"]
                        ];
    CGFloat w = self.view.bounds.size.width;
    // 本地加载 --- 创建不带标题的图片轮播器
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, w, 132) imagesGroup:images];
    cycleScrollView.contentMode = UIViewContentModeScaleAspectFit;
    cycleScrollView.infiniteLoop = YES;
    cycleScrollView.delegate = self;
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    //    [self.view addSubview:cycleScrollView];
    // --- 轮播时间间隔，默认1.0秒，可自定义
    cycleScrollView.autoScrollTimeInterval = 1.5;
    [view addSubview:cycleScrollView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 135, 20, 20)];
    imageView.image = [UIImage imageNamed:@"音标"];
    [view addSubview:imageView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(45, 135, 200, 30);
    [btn setTitle:@"设置常去影院" forState:UIControlStateNormal];
    [btn setTintColor:[UIColor orangeColor]];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    UIView *sepView = [[UIView alloc] initWithFrame:CGRectMake(0, 165, kScreenWidth, 5)];
    //    sepView.backgroundColor = [UIColor colorWithWhite:0.749 alpha:1.000];
    sepView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"xuxian"]];
    [view addSubview:sepView];
    
    return view;
}


/*============================================tableView===============================================*/
#pragma mark - 触发方法
/**
 *  轮播图点击触发方法
 *
 *  @param cycleScrollView 轮播图自动滚动
 *  @param index           轮播图的下标
 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", index);
}

- (void)btnAction:(UIButton *)button
{
    NSLog(@"点击了常去影院");
}

/*============================================tableView===============================================*/
#pragma mark - tableView协议方法
// tableView有多少个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

// 每个分组内有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _cinemaArray.count;
}

// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell1";
    [tableView registerClass:[FPCinema_TableViewCell class] forCellReuseIdentifier:cellIdentifier];
    FPCinema_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    
    FPCinemaModel *model        = [[FPCinemaModel alloc]init];
    model                       = self.cinemaArray[indexPath.row];
    FPCinemaModel *feature      = [[FPCinemaModel alloc] init];
    feature                     = self.featureArray[indexPath.row];
    cell.cinemaNameLabel.text   = model.cinameName;
    cell.addressLabel.text      = model.address;
    if ([[NSString stringWithFormat:@"%d",model.minPrice] length] > 2) {
        NSRange range = {0,2};
        cell.priceLabel.text    = [[NSString stringWithFormat:@"%d",model.minPrice] substringWithRange:range];
    }else
    {
        cell.priceLabel.text    = [NSString stringWithFormat:@"%d",model.minPrice];
    }
    feature.hasPark ? (cell.parkImageView.hidden = NO) : (cell.parkImageView.hidden = YES);
    feature.hasWifi ? (cell.wifiImageView.hidden = NO) : (cell.wifiImageView.hidden = YES);
    feature.has3D ? (cell.thirdDImageView.hidden = NO) : (cell.thirdDImageView.hidden = YES);
    feature.hasServiceTicket ? (cell.ATSImageView.hidden = NO) : (cell.ATSImageView.hidden = YES);
    feature.hasVIP ? (cell.vipImageView.hidden = NO) : (cell.vipImageView.hidden = YES);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

// tableView点击cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FPDetailCinemaTableViewController *fpDetailCinemaTC = [[FPDetailCinemaTableViewController alloc] init];
    FPCinemaModel *model = self.cinemaArray[indexPath.row];
    fpDetailCinemaTC.kUrl = [NSString stringWithFormat:@"%d",model.cinemaId];
   [self.navigationController pushViewController:fpDetailCinemaTC animated:YES];
    
}

/*=========================================collectionView=============================================*/
#pragma mark - collectionView协议方法
// 返回cell宽和高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenWidth / 3 - 7, 30);
}

// 一共多少个分组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 每个分组多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _titleArray.count;
}

#pragma mark - 返回cell
// 返回collectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell2";
    [collectionView registerClass:[FPCinemaCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
    FPCinemaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    cell.label.text = self.titleArray[indexPath.item];
    
    if (_isClick == YES) {
        cell.label.backgroundColor = [UIColor grayColor];
    }else{
        cell.label.backgroundColor = [UIColor whiteColor];
    }
    
    (indexPath.item == 0) ? (cell.label.backgroundColor = [UIColor grayColor]) : (cell.label.backgroundColor = [UIColor whiteColor]);
    return cell;
}

// collection点击cell方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    _isClick = NO;
    for (FPCinemaCollectionViewCell *cell in collectionView.visibleCells) {
            cell.label.backgroundColor = [UIColor whiteColor];
    }
    
    NSLog(@"%ld",indexPath.item);
    _isClick = YES;
    FPCinemaCollectionViewCell *cell = (FPCinemaCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.label.backgroundColor = [UIColor grayColor];
    
    switch (indexPath.row) {
        case 0:
            // 显示所有信息
            NSLog(@"全部");
            break;
        case 1:
            // 显示附近电影院
            NSLog(@"附近");
            break;
        case 2:
            // 显示价格
            NSLog(@"价格");
            break;
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
